#include "widget.h"
#include "ui_widget.h"

#include <QColorDialog>
#include <QMessageBox>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
//    mPMaskLayer = new PMaskLayer(this);
//    mPMaskLayer->setFixedSize(this->size());
//    mPMaskLayer->setVisible(false);
//    this->stackUnder(mPMaskLayer);

//    mPMaskLayer->setAutoFillBackground(true);
//    QPalette pal = mPMaskLayer->palette();
//    pal.setColor(QPalette::Background, QColor(0x00, 0x00, 0x00, 0x20));
//    mPMaskLayer->setPalette(pal);
//    mPMaskLayer->setFixedSize(this->size());



    mNewLabel = new SignatureLabel(this);
    mNewLabel->setBackgroundTransparent(true);
    ui->verticalLayout->addWidget(mNewLabel);


    QStringList list;
    list << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10" << "11" << "12";
    ui->comboBox->addItems(list);
    ui->comboBox->setCurrentIndex(1);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnSelectColor_clicked()
{
    // 获取颜色对话框选择的颜色
    QColor color = QColorDialog::getColor(mNewLabel->penColor(), this, "选择画笔颜色");

    mNewLabel->setPenColor(color);
    ui->lblColor->setStyleSheet(QString("background-color:rgb(%1, %2, %3)")
                                .arg(color.red()).arg(color.green()).arg(color.blue()));
}

void Widget::on_btnSaveImage_clicked()
{
    if (!mNewLabel->hasSignature()) {
        QMessageBox::information(this, "提示", "您还没有签名！");
        return;
    }

    QPixmap pixmap = mNewLabel->signatureImage();
    bool flag = pixmap.save("name.png", "PNG");

    QImage image = mNewLabel->signatureImageHQ();
    flag = image.save("name1.png", "PNG");

    if (flag) {
        QMessageBox::information(this, "提示", "签名保存成功！");
    } else {
        QMessageBox::information(this, "提示", "签名保存失败！");
    }
}

void Widget::on_btnClear_clicked()
{
    mNewLabel->clearSignature();
}

void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
{
    mNewLabel->setPenWidth(arg1.toInt());
}

void Widget::on_btnUndo_clicked()
{
    mNewLabel->undo();
}
